package com.tg.git;


import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.notes.Note;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;
import java.io.IOException;

public class JgitTest {
    //定义本地git路径
    public static final String LOCALPATH = "D:/demo/";
    //.git文件路径
    public static final String LOCALGITFILE = LOCALPATH + ".git";
    //远程仓库地址
    public static final String REMOTEREPOURI = "https://gitee.com/zero1036/tgor-book.git";//操作git的用户名
    public static final String USER = "zero1036";
    //密码
    public static final String PASSWORD = "zero.0.7tgog";
    //git远程仓库服务器ip
    public static final String HOST = "172.16.124.101";

    //建立与远程仓库的联系，仅需要执行一次
    public static String setupRepo() {
        String msg = "";
        try {
            Git git = Git.cloneRepository()
                    .setURI(REMOTEREPOURI)
                    .setCredentialsProvider(new UsernamePasswordCredentialsProvider(USER, PASSWORD))
                    .setBranch("master").setDirectory(new File(LOCALPATH)).call();
            msg = "git init success！";
        } catch (Exception e) {
            msg = "git已经初始化！";
        }
        return msg;
    }

    public static boolean pullBranchToLocal() {
        boolean resultFlag = false;
        //git仓库地址
        Git git;
        try {
            git = new Git(new FileRepository(LOCALGITFILE));
            git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(USER, PASSWORD)).call();
            resultFlag = true;
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
        return resultFlag;
    }

    private static final String LOCAL_PATH_DOC = "D:\\Project\\tg-java\\";
    public static final String LOCAL_PATH_DOC_GIT_FILE = LOCAL_PATH_DOC + ".git";


    public static boolean test2() {
        boolean resultFlag = false;
        //git仓库地址
        Git git;
        try {
            git = new Git(new FileRepository(LOCAL_PATH_DOC_GIT_FILE));

            Status status = git.status().call();
            System.out.println(status.getChanged());

            git.add().addFilepattern("src/main/java/com/tg/git/.").call();
            git.commit().setMessage("test jgit").call();



            git.log().call().forEach(revCommit -> {

                ObjectId oid = revCommit.getId();
                String id = oid.toString();
                System.out.println(id);



                System.out.println("msg:"+revCommit.getShortMessage()+";time:"+revCommit.getCommitTime());
            });

            //>git show HEAD~4:src/main/java/com/tg/git/JgitTest.java
            Note note = git.notesShow().setNotesRef("HEAD~4").call();

            resultFlag = true;
        } catch (IOException | GitAPIException e) {
            e.printStackTrace();
        }
        return resultFlag;
    }

    public static void main(String[] args) {

//        setupRepo();

//        pullBranchToLocal();

        test2();
    }
}
